#include <logbase.h>
#include <fileOpt.h>

#define MY_CHK_FILE_NAME "/tmp/myWrFile.txt"

typedef struct
{
    pthread_mutex_t chkFock;
    char *chkBuf;
    const char *chkFile;
} myChkInfo;

void myChkWR_Init(myChkInfo *info)
{
    pthread_mutex_init(&info->chkFock, NULL);
    info->chkBuf = NULL;
    info->chkFile = MY_CHK_FILE_NAME;
}

void myChkWrtFileGoto(myChkInfo *info)
{
    // lock
    // malloc
    // file
    pthread_mutex_lock(&info->chkFock);
    info->chkBuf = (char *)malloc(128);
    FILE *tmpfile = fopen(info->chkFile, "w+");

    if (info->chkBuf == NULL)
    {
        logerrorc("malloc error");
        goto finishPtr;
    }
    if (!tmpfile)
    {
        logerrorc("open file error");
        goto finishPtr;
    }
#define MY_TMP_SAVE_STRING "my check str write!\n"
    memcpy(info->chkBuf, MY_TMP_SAVE_STRING, sizeof(MY_TMP_SAVE_STRING));
    fprintf(tmpfile, "%s", info->chkBuf);
    logdebugc("check write file finish:%s", info->chkBuf);
finishPtr:
    pthread_mutex_unlock(&info->chkFock);
    if (info->chkBuf)
    {
        free(info->chkBuf);
        info->chkBuf = NULL;
    }
    if (tmpfile)
    {
        fclose(tmpfile);
        tmpfile = NULL;
    }
}

void myChkReadFileGoto(myChkInfo *info)
{
    // lock
    // malloc
    // file
    pthread_mutex_lock(&info->chkFock);
    info->chkBuf = (char *)malloc(128);
    FILE *tmpfile = fopen(info->chkFile, "r");

    if (info->chkBuf == NULL)
    {
        logerrorc("malloc error");
        goto finishPtr;
    }
    if (!tmpfile)
    {
        logerrorc("open file error");
        goto finishPtr;
    }
    // fscanf(tmpfile, "%128s", info->chkBuf);//只能得到空格之前的字符串，scanf以空格分割每个元素
    fread(info->chkBuf, 1, 128, tmpfile);
    logdebugc("check read file finish:%s", info->chkBuf);
finishPtr:
    pthread_mutex_unlock(&info->chkFock);
    if (info->chkBuf)
    {
        free(info->chkBuf);
        info->chkBuf = NULL;
    }
    if (tmpfile)
    {
        fclose(tmpfile);
        tmpfile = NULL;
    }
}

void myChkWrtFileDo(myChkInfo *info)
{
    // lock
    // malloc
    // file
    pthread_mutex_lock(&info->chkFock);
    info->chkBuf = (char *)malloc(128);
    FILE *tmpfile = fopen(info->chkFile, "w+");
    do
    {
        if (info->chkBuf == NULL)
        {
            logerrorc("malloc error");
            break;
        }
        if (!tmpfile)
        {
            logerrorc("open file error");
            break;
        }
#define MY_TMP_SAVE_STRING "my check str write!\n"
        memcpy(info->chkBuf, MY_TMP_SAVE_STRING, sizeof(MY_TMP_SAVE_STRING));
        fprintf(tmpfile, "%s", info->chkBuf);
        logdebugc("check write file finish:%s", info->chkBuf);
    } while (0);

    pthread_mutex_unlock(&info->chkFock);
    if (info->chkBuf)
    {
        free(info->chkBuf);
        info->chkBuf = NULL;
    }
    if (tmpfile)
    {
        fclose(tmpfile);
        tmpfile = NULL;
    }
}

void myChkReadFileDo(myChkInfo *info)
{
    // lock
    // malloc
    // file
    pthread_mutex_lock(&info->chkFock);
    info->chkBuf = (char *)malloc(128);
    FILE *tmpfile = fopen(info->chkFile, "r");
    do
    {
        if (info->chkBuf == NULL)
        {
            logerrorc("malloc error");
            break;
        }
        if (!tmpfile)
        {
            logerrorc("open file error");
            break;
        }
        // fscanf(tmpfile, "%128s", info->chkBuf);//只能得到空格之前的字符串，scanf以空格分割每个元素
        fread(info->chkBuf, 1, 128, tmpfile);
        logdebugc("check read file finish:%s", info->chkBuf);
    } while (0);

    pthread_mutex_unlock(&info->chkFock);
    if (info->chkBuf)
    {
        free(info->chkBuf);
        info->chkBuf = NULL;
    }
    if (tmpfile)
    {
        fclose(tmpfile);
        tmpfile = NULL;
    }
}

int main(int argc, char **argv)
{
    myChkInfo tmpchk;
    myChkWR_Init(&tmpchk);
    logdebugc("================>check goto");
    system("rm -f " MY_CHK_FILE_NAME);
    myChkReadFileGoto(&tmpchk);
    myChkWrtFileGoto(&tmpchk);
    myChkReadFileGoto(&tmpchk);

    logdebugc("================>check do");
    system("rm -f " MY_CHK_FILE_NAME);
    myChkReadFileDo(&tmpchk);
    myChkWrtFileDo(&tmpchk);
    myChkReadFileDo(&tmpchk);
    return 1;
}
